package com.tianyang.yukuaifuzs.business.auth.fragment;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.base.BaseFragment;
import com.tianyang.yukuaifuzs.business.auth.adapter.MerchantSubmitAdapter;
import com.tianyang.yukuaifuzs.business.auth.contract.MerchantRefuseContract;
import com.tianyang.yukuaifuzs.business.auth.presenter.MerchantRefusePresenter;
import com.tianyang.yukuaifuzs.business.auth.provider.AuthOpenProvider;
import com.tianyang.yukuaifuzs.business.main.bean.PosUserOutput;
import com.tianyang.yukuaifuzs.common.interfaces.CommonConfig;
import com.tianyang.yukuaifuzs.module.dialog.HintDialog;
import com.tianyang.yukuaifuzs.module.dialog.MerchantTypeDialog;
import com.tianyang.yukuaifuzs.module.dialog.interfaces.ICallbackListener;
import com.tianyang.yukuaifuzs.module.widget.VerticalSwipeRefreshLayout;
import com.tianyang.yukuaifuzs.utils.SoftInputUtil;

import java.util.List;

import butterknife.BindView;

/**
 * Description：商户已拒绝
 * Created by xyj on 2024/3/8
 */
public class MerchantRefuseFragment extends BaseFragment implements MerchantRefuseContract.View {

    @BindView(R.id.phoneNumberEt)
    EditText phoneNumberEt;
    @BindView(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView vRecyclerview;
    @BindView(R.id.emptyView)
    RelativeLayout emptyView;

    private MerchantRefuseContract.Presenter mPresenter;

    private MerchantSubmitAdapter vMerchantSubmitAdapter;

    private MerchantTypeDialog vMerchantTypeDialog;

    private int nowPage = CommonConfig.PAGE_NUM;

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_merchant_submit;
    }


    @Override
    protected void initView() {
        super.initView();
        vRecyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        vMerchantSubmitAdapter = new MerchantSubmitAdapter(true);
        vRecyclerview.setAdapter(vMerchantSubmitAdapter);
        vMerchantSubmitAdapter.disableLoadMoreIfNotFullPage(vRecyclerview);
        vMerchantSubmitAdapter.setPreLoadNumber(2);

        mPresenter = new MerchantRefusePresenter(this);
        mPresenter.getMerchantRefuseList(nowPage, phoneNumberEt.getText().toString());
    }

    @Override
    protected void setListener() {
        super.setListener();
        phoneNumberEt.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    if (getActivity() != null) SoftInputUtil.hideSoftInput(getActivity());
                    nowPage = CommonConfig.PAGE_NUM;
                    mPresenter.getMerchantRefuseList(nowPage, phoneNumberEt.getText().toString());
                    vMerchantSubmitAdapter.setEnableLoadMore(true);
                }
                return false;
            }
        });
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                nowPage = CommonConfig.PAGE_NUM;
                mPresenter.getMerchantRefuseList(nowPage, phoneNumberEt.getText().toString());
                vMerchantSubmitAdapter.setEnableLoadMore(true);
            }
        });
        vMerchantSubmitAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getMerchantRefuseList(nowPage, phoneNumberEt.getText().toString());
            }
        }, vRecyclerview);
        vMerchantSubmitAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                PosUserOutput output = ((MerchantSubmitAdapter) adapter).getItem(position);
                if (output != null) {
                    if (view.getId() == R.id.updateTypeRtv) {
                        showMerchantTypeDialog(output.getPhone(), output.getMchType() + "");
                    } else if (view.getId() == R.id.hintTv && !TextUtils.isEmpty(output.getFailResult())) {
                        new HintDialog(getContext())
                                .title("驳回原因")
                                .content(output.getFailResult())
                                .show();
                    }
                }
            }
        });
        vMerchantSubmitAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                PosUserOutput output = ((MerchantSubmitAdapter) adapter).getItem(position);
                if (output != null) {
                    AuthOpenProvider.getInstance().openAuthByMchType(getContext(), output.getMchType(),
                            String.valueOf(output.getPosStatus()), false, output.getPhone(), true);
                }
            }
        });
    }

    /**
     * Description:显示修改商户类型弹框
     */
    private void showMerchantTypeDialog(String tel, String mchType) {
        if (vMerchantTypeDialog == null) {
            vMerchantTypeDialog = new MerchantTypeDialog(getContext());
        }
        vMerchantTypeDialog.setListener(new ICallbackListener<String>() {
            @Override
            public void callback(String merchantType) {
                mPresenter.saveIdCertTemp(tel, merchantType);
            }
        });
        vMerchantTypeDialog.setMerchantType(mchType);
        vMerchantTypeDialog.show();
    }

    @Override
    public void showSaveSuccess() {
        nowPage = CommonConfig.PAGE_NUM;
        mPresenter.getMerchantRefuseList(nowPage, phoneNumberEt.getText().toString());
        vMerchantSubmitAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showMerchantRefuseListView(List<PosUserOutput> outputs, boolean isFirst) {
        if (outputs != null && outputs.size() > 0) {
            if (isFirst) {
                vMerchantSubmitAdapter.setNewData(outputs);
            } else {
                vMerchantSubmitAdapter.addData(outputs);
            }
            nowPage++;
            vRecyclerview.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
        } else if (isFirst) {
            vRecyclerview.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                vMerchantSubmitAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                vMerchantSubmitAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                vMerchantSubmitAdapter.loadMoreFail();
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(MerchantRefuseContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
